<template>
  <div id="login-page">
    <div id="login-form">
      <div id="logo"></div>
      <el-form :model="loginForm" status-icon ref="loginForm" :rules="rules" label-width="50px" class="demo-ruleForm">
        <el-form-item label="账号" prop="loginName">
          <el-input v-model="loginForm.loginName" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" type="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" :show-password="true" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 280px;" @click="login('loginForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CarSharingLogin',
  data () {
    return {
      loginForm: {
        loginName: '',
        password: ''
      },
      rules: {
        loginName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 登录
    login (forName) {
      this.$refs[forName].validate((valid) => {
        if (valid) {
          // this.$axios.post('/backstageLogin/login', this.loginForm).then((result) => {
          //   if (result.data.flag) {
          //     // 将token令牌以及用户名放入session
          //     window.sessionStorage.setItem('token', result.data.obj)
          //     // window.sessionStorage.setItem('loginName', result.data.obj.loginName)
          //     setTimeout(() => {
          //       this.$router.push('/home')
          //       // this.myMessage('success', '登录成功！欢迎尊敬的' + result.data.obj.roleName + '进入系统')
          //     }, 500)
          //     this.resetForm(forName)
          //   } else {
          //     this.myMessage('warning', result.data.obj)
          //     this.resetForm(forName)
          //   }
          // }).catch((err) => {
          //   console.log(err)
          //   this.myMessage('error', '系统错误，请联系管理员！')
          // })
          // this.$router.push('/BookIndex')
          this.$axios.post('/user/login', this.loginForm).then(res => {
            // this.$router.push('/BookIndex')
            if (res.data.flag) {
              debugger
              const token = res.data.obj
              //  localStorage 存储在本地
              //  sessionStorage 存储在回话当中 当关闭浏览器 或浏览器页面时 会话被删除
              window.sessionStorage.setItem('token', token)
              localStorage.setItem('token', token)
              this.$router.push('/BookIndex')
            } else {
              this.myMessage('error', res.data.obj)
            }
          })
        }
      })
    },
    // 提示信息
    myMessage (type, message) {
      this.$message({
        showClose: true,
        message: message,
        type: type
      })
    },
    // 重置表单
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style  scoped>
  .demo-ruleForm{
    margin: 0 auto;
  }
  #login-page{
    width: 100%;
    height: 100%;
    background: url(../../assets/image/宇宙.jpg) no-repeat center center;
    background-size: 100% 100%;
  }
  #login-form{
    width: 400px;
    height: 450px;
    background: rgba(255,255,255,.8);
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -280px;
    margin-left: -200px;
    box-sizing: border-box;
  }
  #logo{
    width: 150px;
    height: 150px;
    border-radius: 50%;
    background: url(../../assets/image/温柔卡卡西.jpg) no-repeat center center;
    background-size: 150px 150px;
    margin: 40px auto 30px;
  }
  /deep/ .el-form-item__label{
    color: #000;
  }
  .el-form{
    padding: 0 30px 0 20px;
  }
</style>
